﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.Utils.Drawing;
using DevExpress.Utils.Frames;
using DevExpress.XtraEditors;
using WinForm.Common;

namespace WinForm.Forms
{
    /// <summary>
    /// 单独定义了静态方法ShowAbout()来显示该窗口，需要的地方可以调用该方法
    /// 类构造函数中加载了背景图片，显示并设置了版本、版权等
    /// 重写了鼠标的事件使得鼠标可以拖动窗口
    /// 重写了OnPain(),对版权版本区域进行重绘
    /// </summary>
    public partial class FormAbout : XtraForm
    {
        /// <summary>
        /// blnMoving，MouseDownX, MouseDownY三个变量控制鼠标拖动窗口
        /// 
        /// </summary>
        private bool blnMoving;
        private int MouseDownX, MouseDownY; 

        /// <summary>
        /// 标签的字体
        /// </summary>
        private static string fontName = "Segoe UI";
        /// <summary>
        /// 显示版本，版权等信息的标签
        /// </summary>
        private LabelInfo labelInfo = new LabelInfo();

        /// <summary>
        /// color1 标签中字体的颜色
        /// color2 标签中分隔符的颜色
        /// </summary>
        private static Color
            color1 = Color.FromArgb(210, 196, 196),
            color2 = Color.FromArgb(143, 133, 127);

        private static AppearanceDefault
            versionAppearance = new AppearanceDefault(color1, Color.Empty, HorzAlignment.Near, VertAlignment.Top,
                new Font(fontName, 9.25f)),
            descriptionAppearance = new AppearanceDefault(color1, Color.Empty, HorzAlignment.Near, VertAlignment.Top,
                new Font(fontName, 7.75f)),
            copyrightAppearance = new AppearanceDefault(color2, Color.Empty, HorzAlignment.Center, VertAlignment.Top,
                new Font(fontName, 7.75f));

        private static Rectangle
            versionBounds = new Rectangle(210, 94, 260, 20),
            descriptionBounds = new Rectangle(210, 124, 270, 100),
            copyrightBounds = new Rectangle(20, 280, 485, 20);

        public FormAbout()
        {
            InitializeComponent();
            ElementConstStringLoader.LoadConstStringsForFrmAbout(this);
            Image bgImage = ResourcesHelper.GetImageFromImagesFolder("About", ImageSize.None);
            BackgroundImage = bgImage;
            Size = bgImage.Size;
            CreateLinks();
        }
        void CreateLinks()
        {
            labelInfo.Font = new Font(fontName, 8.75f);
            labelInfo.BackColor = Color.Transparent;
            if (LocalizationHelper.IsJapanese) 
                labelInfo.Bounds = new Rectangle(155, 260, 430, 20);
            else
                labelInfo.Bounds = new Rectangle(55, 260, 131, 20);//其中的width需要根据实际长度调整，否则单击到labelInFo
            labelInfo.Parent = this;
            labelInfo.ItemClick += OnLabelClick;
            if (!LocalizationHelper.IsJapanese)
            {
                labelInfo.Texts.Add(ResourcesHelper.GetString("AboutUs"), color1, true);
            }
            labelInfo.Texts.Add(ResourcesHelper.GetString("AboutFormSeparator"), color2);
            labelInfo.Texts.Add(ResourcesHelper.GetString("ContactUs"), color1, true);
        }
        

        /// <summary>
        /// 标签 点击处理程序，获得点击的部分，然后显示相应的网站
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void OnLabelClick(object sender, LabelInfoItemClickEventArgs e)
        {
            string name = GetProcessName(e);
            if (name != null)
                ObjectHelper.ShowWebSite(name);
            
        }

        private string GetProcessName(LabelInfoItemClickEventArgs e)
        {
            //if (e.InfoText.Text.Equals(ResourcesHelper.Get("AboutUs"))) return ResourcesHelper.Get("AboutUsLink");
            if (e.InfoText.Text.Equals(ResourcesHelper.GetString("AboutUs"))) return ResourcesHelper.GetString("AboutUsLink");
            if (e.InfoText.Text.Equals(ResourcesHelper.GetString("ContactUs"))) return ResourcesHelper.GetString("ContactUsLink");
            return null;
        }

        /// <summary>
        /// 显示“关于”窗口
        /// 需要显示该窗口时调用该静态方法，显示完就释放资源。
        /// </summary>
        /// <param name="parent">父窗口</param>
        public static void ShowAbout(Form parent)
        {
            FormAbout about = new FormAbout();
            about.Opacity = 0; //窗体的不透明级别，0：完全透明，1：不透明
            Timer tmr = new Timer();
            tmr.Tag = about;
            tmr.Interval = 20;
            tmr.Tick += tmr_Tick;
            tmr.Start();
            about.ShowDialog(parent);
            about.Dispose();
        }

        /// <summary>
        /// 窗体渐变不透明
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void tmr_Tick(object sender, EventArgs e)
        {
            Timer tmr = sender as Timer;
            Form frm = tmr.Tag as Form;
            if (frm == null)
            {
                tmr.Stop();
                return;
            }
            try
            {
                frm.Opacity += 0.07;
                if (frm.Opacity >= 0.99) tmr.Tag = null;
            }
            catch
            {
            }
        }


        #region override重写的方法

        /// <summary>
        /// ESC键关闭窗体
        /// </summary>
        /// <param name="e"></param>
        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
            {
                e.Handled = true;
                Close(); //
            }
            base.OnKeyDown(e);
        }

        /// <summary>
        /// 在上部分（e.Y 小于 250)单击鼠标键关闭窗口或者在下部分单击左键开始移动窗口
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            if (e.Y < 250)
            {
                Close();
            }
            else
            {
                if (e.Button == MouseButtons.Left)
                {
                    blnMoving = true;
                    MouseDownX = e.X;
                    MouseDownY = e.Y;
                }
            }
        }

        /// <summary>
        /// 拖动鼠标移动窗口
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            if (blnMoving)
            {
                Location = new Point(Location.X + (e.X - MouseDownX), Location.Y + (e.Y - MouseDownY));
            }
        }

        /// <summary>
        /// 松开鼠标左键，停止移动窗口 
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            if (e.Button == MouseButtons.Left)
            {
                blnMoving = false;
            }
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            //base.OnPaint(e);
            using (var cache = new GraphicsCache(e))
            {
                cache.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                DrawVersion(cache);
                DrawDescription(cache);
                DrawCopyright(cache);
            }
        }

        #endregion

        #region 绘制版本、描述、版权函数
        void DrawVersion(GraphicsCache cache)
        {
            AppearanceObject app = new AppearanceObject(versionAppearance);
            //app.DrawString(cache, string.Format(ResourcesHelper.Get("Version"), AssemblyInfo.MarketingVersion), versionBounds);
            app.DrawString(cache, string.Format("{0}: {1}",ResourcesHelper.GetString("Version"), ResourcesHelper.GetString("VersionValue")), versionBounds);
        }
        void DrawDescription(GraphicsCache cache)
        {
            //AppearanceObject app = new AppearanceObject(descriptionAppearance);
            //app.TextOptions.WordWrap = WordWrap.Wrap;
            //app.DrawString(cache, ResourcesHelper.Get("AboutFormDescription"), descriptionBounds);
        }
        void DrawCopyright(GraphicsCache cache)
        {
            AppearanceObject app = new AppearanceObject(copyrightAppearance);
            app.DrawString(cache, string.Format(ResourcesHelper.GetString("Copyright"), DateTime.Now.Year), copyrightBounds);
        }
        #endregion 绘制版本、描述、版权函数
    }
}
            